angular.module('usercenter.module.mywealth', [])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider) {
        $stateProvider
            .state('mywealth', {
                url: '/mywealth',
                templateUrl: uc_templateprefix + 'mywealth/mywealth.html',
                controller: 'MywealthController'
            })
            .state('zjsq', {
                url: '/zjsq',
                templateUrl: uc_templateprefix + 'mywealth/zjsq.html',
                controller: 'ZjsqController'
            })
            .state('article', {
                url: '/article',
                templateUrl: uc_templateprefix + 'mywealth/article.html',
                controller: 'ArticleController'
            })
            .state('surepay', {
                url: '/surepay',
                templateUrl: uc_templateprefix + 'mywealth/surepay.html',
                controller: 'SurepayController'
            })
            .state('newpaymentpassword', {
                url: '/newpaymentpassword',
                templateUrl: uc_templateprefix + 'mywealth/newpaymentpassword.html',
                controller: 'NewPaymentPasswordController'
            })
            .state('setpaymentpassword', {
                url: '/setpaymentpassword',
                templateUrl: uc_templateprefix + 'mywealth/setpaymentpassword.html',
                controller: 'SetPaymentPasswordController'
            })
            .state('revisepaymentpassword', {
                url: '/revisepaymentpassword',
                templateUrl: uc_templateprefix + 'mywealth/revisepaymentpassword.html',
                controller: 'RevisePaymentPasswordController'
            })
            .state('retrievepaymentpassword', {
                url: '/retrievepaymentpassword',
                templateUrl: uc_templateprefix + 'mywealth/retrievepaymentpassword.html',
                controller: 'RetrievePaymentPasswordController'
            })
    }])

    .controller('MywealthController', ['$scope', '$state', 'AuthService', function ($scope, $state, AuthService) {
        $scope.username = AuthService.getUsername();
        $scope.balance = 0.00;
        $scope.userblance = {};
        AuthService.Userbalance().$promise.then(function (data) {
            $scope.userblance = data;
            $scope.balance = data.balance;
        }, function (error) {
            //�?
        });
        $scope.paymentpassword = function () {
            if($scope.userblance.paysecret){
                if ($scope.userblance.paysecret == null || $scope.userblance.paysecret == '') {
                    $state.go('setpaymentpassword');
                }else{
                    $state.go('newpaymentpassword');
                }
            }else{
                $state.go('newpaymentpassword');
            }
        }
    }])

    .controller('ZjsqController', ['$scope', 'AuthService', function ($scope, AuthService) {
        $scope.username = AuthService.getUsername();
        $scope.pushup = function () {
            var det = $(".detail-2").val();
            var flag = false;
            if (det == '') {
                input_alert("请填写正确信息！");
            }
            else {
                flag = true;
            }
            return flag;
        }
    }])
    .controller('ArticleController', ['$scope','$state', 'AuthService', function ($scope,$state, AuthService) {
        //$scope.username = AuthService.getUsername();
        $scope.recharge = function(){
            $state.go("surepay");
            var userid = AuthService.getUserid();
            var backurl = projectBaseUrl + "mywealth";
            var openid = $storage.wxpay_openid;
            window.location.href = payplateurl(10002,userid,backurl,openid);
        }
    }])

    .controller('SurepayController', ['$scope', '$window', 'AuthService', 'WxRecharge', 'AliRecharge', function ($scope, $window, AuthService, WxRecharge, AliRecharge) {
        bindKeyEvent($(".bac_14"))
        function choose_pay() {
            var payWay = $('input:radio:checked').val();
            if (payWay == 1) {
                $scope.way = 'wx';
            } else if (payWay == 2) {
                $scope.way = 'ali';
            }
        }

        $scope.environment = false;
        //判断是否满足微信支付环境
        if (typeof WeixinJSBridge != "undefined") {
            $scope.environment = true;
        } //默认支付方式
        if ($scope.environment) {
            $('#paythree1').attr("checked", "checked")
        } else {
            $('#paythree2').attr("checked", "checked")
        }
        $scope.change = function () {
            if ($scope.rechargeOrder.payment != null && $scope.rechargeOrder.payment > 0) {
                document.getElementById("regedit1").removeAttribute("disabled");
                document.getElementById("regedit1").style.backgroundColor = 'rgb(48, 174, 79)';
                document.getElementById("regedit1").style.cursor = "pointer";
            }
            else {
                document.getElementById("regedit1").setAttribute("disabled", "disabled");
                document.getElementById("regedit1").style.backgroundColor = 'rgb(94,94,94)';
                document.getElementById("regedit1").style.cursor = "not-allowed";
            }
        }
        $scope.rechargeOrder = {};//声明rechargeOrder 是一个对�?
        $scope.rechargeOrder.platform = 0;//0表示农归�? 1表示餐谋�?
        $scope.rechargeOrder.userid = AuthService.getUserid();
        $scope.rechargeOrder.wxspid = uc_payconfig.wxpay_spid;
        $scope.rechargeOrder.return_url = "http://nonggui8.com/mobile/mywealth";
        $scope.recharge = function () {
            choose_pay();
            if ($scope.way == "wx") {//微信支付
                function success_func(result) {
                    //alert(JSON.stringify(result));
                    WeixinJSBridge.invoke('getBrandWCPayRequest', {
                        "appId": result.appID,//"wx2421b1c4370ec43b", //公众号名称，由商户传�?
                        "timeStamp": result.timeStamp,//"1395712654", //时间戳，�?1970年以来的秒数
                        "nonceStr": result.nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机�?
                        "package": result.pacKage,//"prepay_id=u802345jgfjsdfgsdg888",
                        "signType": result.signType,//"MD5", //微信签名方式:
                        "paySign": result.paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
                    }, function (res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返�?    ok，但并不保证它绝对可靠�??
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            $state.go('mywealth');
                        }
                        if (res.err_msg == "get_brand_wcpay_request:cancel") {
                            alert("交易取消");
                        }
                        if (res.err_msg == "get_brand_wcpay_request:fail") {
                            alert("支付失败");
                        }
                    });
                }

                WxRecharge.wxrechargePay($scope.rechargeOrder, success_func);
            }

            if ($scope.way == "ali") {//阿里支付
                AliRecharge.alirechargePay($scope.rechargeOrder).then(function (data) {
                    $window.location.href = data;
                });
            }
        }
    }])

    //设置支付密码
    .controller('SetPaymentPasswordController', ['$scope', 'AuthService', function ($scope, AuthService) {
        /*password();*/
        password(".fake-box1 input", "#pwd-input", "send_set");
        $scope.userpay = function () {
            $scope.paypassword = {};
            $scope.paypassword.paysecret = hex_md5($scope.paysecret);
            AuthService.Userpaypassword($scope.paypassword,
                function () {
                    input_alert("密码设置成功", 2000)
                    setTimeout(function () {
                        window.history.go(-1);
                    }, 2000)
                }, function (data) {
                    $scope.result = data.data.reason;
                    input_alert($scope.result)
                });
        }

    }])
    //管理支付密码
    .controller('NewPaymentPasswordController', ['$scope', function ($scope) {

    }])
    //修改支付密码
    .controller('RevisePaymentPasswordController', ['$scope', 'AuthService', function ($scope, AuthService) {
        password(".fake-box2 input", "#pwd-input2", "send_set2");
        password(".fake-box3 input", "#pwd-input3", "send_set3");
        $("#pwd-input2").focus();
        AuthService.Userbalance().$promise.then(function (data) {
            $scope.paysecret = data.paysecret;
        }, function (error) {
            //�?
        })
        $scope.next = function () {
            //$scope.oldpaysecret = hex_md5($scope.oldpaysecret);
            $scope.newpaypassword = {};
            if ($scope.oldpaysecret == $scope.paysecret) {
                $scope.newpaypassword.paysecret = $scope.oldpaysecret;
                click_show(".allpwd-box", ".newpwd-box", "#hidval");
                $("#pwd-input3").focus();
            } else {
                input_alert("原始密码错误")
                $("#pwd-input2").select()
                $("#pwd-input2").focus();
            }
        }
        $scope.usernewpay = function () {
            //$scope.newpaypassword.newpaysecret = hex_md5($scope.newpaysecret);
            $scope.newpaypassword.newpaysecret = $scope.newpaysecret;
            AuthService.Resetpassword($scope.newpaypassword,
                function () {
                    input_alert("修改成功", 2000)
                    setTimeout(function () {
                        window.history.go(-1);
                    }, 2000)
                }, function (data) {
                    input_alert(data)
                });
        }
    }])
    //修改支付密码
    .controller('RetrievePaymentPasswordController', ['$scope', 'AuthService', function ($scope, AuthService) {
        password(".fake-box3 input", "#pwd-input3", "send_set3");
        $scope.getcode = function (phone) {
            var nexId = document.getElementById('send_set2');
            nexId.removeAttribute('disabled', 'disabled');
            nexId.style.backgroundColor = '#30AE4F';
            var ph = $(".tel").val();
            var flag = false;
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if (ph == '') {
                input_alert("手机号码不能为空！");
                $(".tel").focus();
            } else if (ph.length != 11) {
                input_alert("请输入有效的手机号码！");
                $(".tel").focus();
                $(".tel").select();
            } else if (!myreg.test(ph)) {
                input_alert("请输入有效的手机号码！");
                $(".tel").focus();
                $(".tel").select();
            } else if (checkPhoneIsExist()) {
                AuthService.getcode(phone, function () {
                    nexId.removeAttribute('disabled', 'disabled');
                    nexId.style.backgroundColor = '#30AE4F';
                });
                settime();
            } else {
                flag = true;
            }
            return flag;
        }
        $scope.next = function () {
            if($scope.user.code == undefined){
                input_alert('请输入验证码')
            }else{
                click_show(".allpwd-box", ".newpwd-box", "#hidval");
            }
        }
        $scope.usernewpay = function (){
            AuthService.RetrievePaymentPassword($scope.user,
                function () {
                    input_alert("修改成功", 2000)
                    setTimeout(function () {
                        window.history.go(-1);
                    }, 2000)
                }, function (data) {
                    input_alert(data.data.reason)
                });
        }
    }])